package com.youbar.client.basic;

import com.youbar.client.enums.ResultCode;

public class BizResult<T> {

    private int code;
    private String message;
    private boolean success;
    private T data;

    public BizResult() {}

    public BizResult(ResultCode code){
        this.code = code.getCode();
        this.message = code.getMessage();
        this.success = code.isSuccess();
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T t) {
        this.data = t;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public static BizResult error(ResultCode code){
        BizResult bizResult = new BizResult();
        bizResult.setMessage(code.getMessage());
        bizResult.setSuccess(false);
        bizResult.setCode(code.getCode());
        return bizResult;
    }

    public static BizResult error(){
        return error(ResultCode.ERROR);
    }

    public static BizResult success(){
        return new BizResult(ResultCode.SUCCESS);
    }

    public static BizResult success(Object data){
        BizResult bizResult =  new BizResult(ResultCode.SUCCESS);
        bizResult.setData(data);
        return bizResult;
    }
    public static BizResult error(Object data){
        BizResult bizResult =  new BizResult(ResultCode.FALSE);
        bizResult.setData(data);
        return bizResult;
    }
}
